#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 210;
int n, m;
int g[N][N];
int s, e;
void floyd()
{
	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
			}
		}
	}
}
int main()
{
	while (cin >> n >> m) {
		memset(g, 0x3f, sizeof(g));
		for (int i = 1; i <= n; i++) {
			g[i][i] = 0;
		}
		int a, b, c;
		while (m--) {
			cin >> a >> b >> c;
			a++;
			b++;
			g[a][b] = g[b][a] = min(g[a][b], c);
		}
		floyd();
		cin >> s >> e;
		s++;
		e++;
		if (g[s][e] == 0x3f3f3f3f) {
			cout << -1 << endl;
		}
		else {
			cout << g[s][e] << endl;
		}
	}
	return 0;
}